package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserActData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class UserActDataDaoImpl extends TempDataObjectDao<UserActData> {

    public static String getUserActDataSql = "select * from `t_u_user_act_data` where `userId`=?";
    public UserActData getUserActData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserActDataSql = "update `t_u_user_act_data` set comGuideActTypes=?,chooseTrueIndexMap=?,updateTime=?,createTime=? where userId=?";
    @Override
    protected boolean updateData(UserActData userActData) {
        userActData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userActData.getComGuideActTypes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userActData.getChooseTrueIndexMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getUserId()));
        if (getDb().executeNoneQuery(updateUserActDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActDataSql = "insert into `t_u_user_act_data`(userId,comGuideActTypes,chooseTrueIndexMap,updateTime,createTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserActData userActData) {
        userActData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userActData.getComGuideActTypes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(userActData.getChooseTrueIndexMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userActData.getCreateTime()));
        if (getDb().executeNoneQuery(insertUserActDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserActData parseData(ResultSet rs) throws SQLException {
        UserActData userActData = new UserActData();
        userActData.setUserId(rs.getLong("userId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("comGuideActTypes"))) {
            userActData.setComGuideActTypes(JSON.parseObject(rs.getString("comGuideActTypes"), new TypeReference<Set<String>>() {}));
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("chooseTrueIndexMap"))) {
            userActData.setChooseTrueIndexMap(JSON.parseObject(rs.getString("chooseTrueIndexMap"), new TypeReference<Map<String, Map<String, String>>>() {}));
        }
        userActData.setUpdateTime(rs.getLong("updateTime"));
        userActData.setCreateTime(rs.getLong("createTime"));
        return userActData;
    }
}
